package org.jash.shop2112.splash

import androidx.databinding.ObservableInt
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.hyphenate.chat.ChatClient
import com.hyphenate.helpdesk.callback.Callback
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jash.shop2112.databinding.ActivitySplashBinding
import org.jash.shop2112.mvvm.BaseActivity
import org.jash.shop2112.mvvm.BaseViewModel
import org.jash.shop2112.utils.log
import kotlin.math.log


class SplashActivity : BaseActivity<ActivitySplashBinding, BaseViewModel>() {
    val count = ObservableInt(3)
    override fun initData() {
        val kv = MMKV.defaultMMKV()
        val first = kv.decodeBool("first", true)
        if (first) {
            lifecycleScope.launch {
                for (i in count.get() downTo 0) {
                    count.set(i)
                    delay(1000)
                }
                ARouter.getInstance()
                    .build("/shop/login")
                    .navigation()
                kv.encode("first", false)
                finish()
            }
        } else {
            ARouter.getInstance()
                .build("/shop/login")
                .navigation()
            finish()
        }
        ChatClient.getInstance().register("user1", "password", object : Callback {
            override fun onSuccess() {
                this@SplashActivity.log("注册成功")
            }

            override fun onError(code: Int, error: String?) {
                this@SplashActivity.log("注册失败: $error")
            }

            override fun onProgress(progress: Int, status: String?) {
                this@SplashActivity.log("注册状态 $status")
            }
        })

    }

    override fun initView() {
        binding.count = count
    }
}